//
//  LMCommunityController.m
//  舍品
//
//  Created by 楼 眉 on 16/2/19.
//  Copyright © 2016年 楼 眉. All rights reserved.
//
#pragma mark - 社区主控制器
#import "LMCommunityController.h"
#import "LMShowViewController.h"
#import "LMPostController.h"
#import "LMPost.h"
#import "LMWarningLabel.h"
#include "LMEvaluationController.h"

#import "LMActionAccountController.h"

@interface LMCommunityController ()<UIScrollViewDelegate>

@property (nonatomic,strong)  UIScrollView *communityTopView;
@property (nonatomic,strong)  UIScrollView *communityBgView;
@property (nonatomic,strong)  UIView *communityBottomLineView;
@property (nonatomic,strong)  NSArray *types;
@end

@implementation LMCommunityController
//懒加载所有帖子类型
- (NSArray *)types
{
    if (_types == nil) {
        _types = @[@"秀一秀",@"评一评"];
    }
    return _types;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupCommunityChildViewControllers];
    [self setupCommunityViews];
    
    //view加载完毕就加载第一个tableview
    [self scrollViewDidEndScrollingAnimation:self.communityBgView];
    
}
#pragma mark - 初始化页面
//初始化页面
- (void)setupCommunityViews
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    //初始化顶部菜单View
    UIScrollView *communityTopView = [[UIScrollView alloc]init];
    communityTopView.frame = CGRectMake(0, 0, SCREEN_WIDTH / 2, 44);
    communityTopView.center = self.view.center;
    communityTopView.showsHorizontalScrollIndicator = NO;
    self.communityTopView = communityTopView;
    [self.view.window addSubview:communityTopView];
    
    CGFloat labelY =0;
    CGFloat labelW = SCREEN_WIDTH / 4;
    CGFloat labelH =communityTopView.height;
    communityTopView.contentSize = CGSizeMake(labelW *self.types.count, 0);
    
    self.navigationItem.titleView = communityTopView;
    
    for (int index = 0; index < self.types.count; index ++) {
        CGFloat labelX = index *labelW;
        UILabel *label = [[UILabel alloc]init];
        label.frame = CGRectMake(labelX, labelY, labelW, labelH);
        label.text = self.types[index];
        label.textColor = RGBCOLOR(100, 100, 100);
        label.font = [UIFont systemFontOfSize:13];
        label.textAlignment = NSTextAlignmentCenter;
        label.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(communityLabelClick:)];
        [label addGestureRecognizer:tapGR];
        [communityTopView addSubview:label];
    }
    
    //初始化下部展示内容View
    CGRectGetMaxY(communityTopView.frame);
    UIScrollView *communityBgView = [[UIScrollView alloc]init];
    communityBgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 20 -44 -49);
    communityBgView.backgroundColor = [UIColor whiteColor];
    communityBgView.delegate = self;
    communityBgView.pagingEnabled = YES;
    communityBgView.showsHorizontalScrollIndicator = NO;
    communityBgView.contentSize = CGSizeMake(SCREEN_WIDTH *self.types.count, 0);
    self.communityBgView = communityBgView;
    communityBgView.y += 64;
    [self.view addSubview:communityBgView];
    
    //下部提示用的红线View
    UIView *communityBottomLineView = [[UIView alloc]init];
    communityBottomLineView.frame = CGRectMake(0, communityTopView.height - 2, communityTopView.width / 2, 2);
    communityBottomLineView.backgroundColor = [UIColor orangeColor];
    self.communityBottomLineView = communityBottomLineView;
    [communityTopView addSubview:communityBottomLineView];
    
    // 初始化右边按钮
    UIButton *rightBtn = [[UIButton alloc]init];
    [rightBtn setImage:[UIImage imageNamed:@"post.png"] forState:UIControlStateNormal];
    rightBtn.frame = CGRectMake(0, 0, 30*kUnit, 30*kUnit);
    [rightBtn addTarget:self action:@selector(rightItemClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}

#pragma mark - 发帖子
- (void)rightItemClick
{
    LMPostController *postVc = [[LMPostController alloc]init];
    
    AFNetworkReachabilityManager *manger = [AFNetworkReachabilityManager sharedManager];
    if (manger.isReachable == NO) {
        [LMWarningLabel LM_createWarningLabelWithTitle:nil];
        return;
    }else
    {
        if ([LMUser currentUser]) {
            [self presentViewController:postVc animated:YES completion:nil];
        }
        else
        {
            [self notLogin];
        }
    }
}
#pragma mark - 尚未登录，跳转注册登录界面
- (void)notLogin
{
    __weak typeof(self) weakSelf = self;
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"您还未登录" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"去登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        LMActionAccountController *loginVc = [[LMActionAccountController alloc]init];
        [weakSelf.navigationController pushViewController:loginVc animated:YES];
    }];
    [alertVc addAction:action];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"先看看" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVc addAction:cancelAction];
    [weakSelf presentViewController:alertVc animated:YES completion:^{
        
    }];

}
#pragma mark - 添加社区各种类型数据控制器
- (void)setupCommunityChildViewControllers
{
    LMShowViewController *childVc0 = [[LMShowViewController alloc]init];
    childVc0.title = self.types[0];
    [self addChildViewController:childVc0];
    
    LMEvaluationController *childVc1 = [[LMEvaluationController alloc]init];
    childVc1.title = self.types[1];
    [self addChildViewController:childVc1];
}

#pragma mark - 类型Label被点击
- (void)communityLabelClick:(UITapGestureRecognizer *)tap
{
    int index = tap.view.x / tap.view.width;
    [self.communityBgView setContentOffset:CGPointMake(index *self.view.width, 0) animated:YES];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    int index = scrollView.contentOffset.x / scrollView.width;
    LMShowViewController *showVc = self.childViewControllers[index];
    UILabel *label = self.communityTopView.subviews[index];
    CGPoint topViewOffset = self.communityTopView.contentOffset;
    topViewOffset.x =  label.center.x - self.view.width / 2;
    CGFloat maxOffsetx = self.communityTopView.contentSize.width - self.view.width;
    if (topViewOffset.x < 0) {
        topViewOffset.x = 0;
    }
    if (topViewOffset.x > maxOffsetx) {
        topViewOffset.x = maxOffsetx;
    }
//    [self.communityTopView setContentOffset:topViewOffset animated:YES];
    if ([showVc isViewLoaded]) {
        return;
    }
    showVc.tableView.frame = CGRectMake(index *scrollView.width, 0, scrollView.width, scrollView.height);
    [scrollView addSubview:showVc.tableView];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.communityBottomLineView.x = scrollView.contentOffset.x / 4;
}

#pragma mark - waring
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark - dealloc
- (void)dealloc
{
    NSLog(@"dealloc--communityVc");
}
@end
